package com.sctvcloud.renhe.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctvcloud.renhe.R;

import static com.sctvcloud.renhe.application.Constances.REBLLION_KEY;

/**
 * Created by Administrator on 2017/6/21 0021.
 */

public class RebllionDiaFragment extends BaseDialogFragment implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup rebllion_type_rg;
    private RadioButton type_rb1, type_rb2, type_rb3, type_rb4, type_rb5;
    public static final int ONE_TAG = 1;
    public static final int TWO_TAG = 2;
    public static final int THREE_TAG = 3;
    public static final int FOUR_TAG = 4;
    public static final int FIVE_TAG = 5;

    @Override
    protected int getContentRes() {
        return R.layout.dia_rebllion_fragment;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        rebllion_type_rg = (RadioGroup) dialog.findViewById(R.id.dia_rebllion_type_rg);
        type_rb1 = (RadioButton) dialog.findViewById(R.id.dia_rebllion_type_rb1);
        type_rb2 = (RadioButton) dialog.findViewById(R.id.dia_rebllion_type_rb2);
        type_rb3 = (RadioButton) dialog.findViewById(R.id.dia_rebllion_type_rb3);
        type_rb4 = (RadioButton) dialog.findViewById(R.id.dia_rebllion_type_rb4);
        type_rb5 = (RadioButton) dialog.findViewById(R.id.dia_rebllion_type_rb5);
        rebllion_type_rg.setOnCheckedChangeListener(this);
        int type = (int) SharedPreferencesUtil.getParam(getActivity(), REBLLION_KEY, 1);
        saveChoiceType(type);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.dia_rebllion_type_rb1:
                choice(ONE_TAG, type_rb1);
                break;
            case R.id.dia_rebllion_type_rb2:
                choice(TWO_TAG, type_rb2);
                break;
            case R.id.dia_rebllion_type_rb3:
                choice(THREE_TAG, type_rb3);
                break;
            case R.id.dia_rebllion_type_rb4:
                choice(FOUR_TAG, type_rb4);
                break;
            case R.id.dia_rebllion_type_rb5:
                choice(FIVE_TAG, type_rb5);
                break;
        }
    }

    private void choice(int type, RadioButton radioButton) {
        if (callBack != null) {
            Message message = new Message();
            message.what = type;
            callBack.onCallBackMsg(this, message);
            radioButton.setChecked(true);
            SharedPreferencesUtil.setParam(getActivity(), REBLLION_KEY, type);
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismissAllowingStateLoss();
                }
            });
        }
    }

    private void saveChoiceType(int type) {
        if (type == 1) {
            type_rb1.setChecked(true);
        } else if (type == 2) {
            type_rb2.setChecked(true);
        } else if (type == 3) {
            type_rb3.setChecked(true);
        } else if (type == 4) {
            type_rb4.setChecked(true);
        } else if (type == 5) {
            type_rb5.setChecked(true);
        }
    }
}
